package com.hospital.diagnose.controller;

import com.alibaba.fastjson.JSON;
import com.hospital.diagnose.entity.Transfer;
import com.hospital.diagnose.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @package_name com.hospital.diagnose.controller
 * @date 2019/1/10 15:55
 * @description
 */
@Controller
@RequestMapping(value = "/transfer")
public class TransferController {
    @Autowired
    TransferService transferService;

    @RequestMapping(value = "/all", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public String getAllTransferInformation() {
        return transferService.getAllInformation();
    }

    @PostMapping(value = "/patName", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String selectByPatName(@RequestBody String patName) {
        if (patName != null) {
            String string = JSON.parseObject(patName).getString("patName");
            System.out.println(string);
            return transferService.selectByPatName(string);
        } else {
            return JSON.toJSONString("该药品无进货");
        }
    }

    @PostMapping(value = "/transTime", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String selectByTransTime(@RequestBody String transTime) throws ParseException {
        String string = JSON.parseObject(transTime).getString("transTime");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time = simpleDateFormat.parse(string);
        System.out.println("11111" + time);
        return transferService.selectByTransTime(time);
    }

    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delOneInfoById(@RequestBody String id1) {
        String string = JSON.parseObject(id1).getString("id");
        Integer id = Integer.parseInt(string);
        return transferService.delOneInfoById(id);
    }

    @PostMapping(value = "/insert", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String insertOneInfo(@RequestBody String data) throws ParseException {
        String patName = JSON.parseObject(data).getString("patName");
        String transTowhere = JSON.parseObject(data).getString("transTowhere");
        String value = JSON.parseObject(data).getString("transTime");
        String value1 = JSON.parseObject(data).getString("transPhysician");
        String value2 = JSON.parseObject(data).getString("transRemark");
        Integer patId = transferService.findPatIdByName(patName);
        System.out.println(value);
        Transfer transfer = new Transfer();
        transfer.setTransPatId(patId);
        transfer.setTransTowhere(transTowhere);
        transfer.setTransTime(new SimpleDateFormat("yyyy-MM-dd").parse(value));
        transfer.setTransPhysician(value1);
        transfer.setTransRemark(value2);
        return transferService.addOneInfo(transfer);
    }

    @PostMapping(value = "/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateOneInfo(@RequestBody String data) throws ParseException {
//        String idList = JSON.parseObject(data).getString("id");
//        Integer id = idList.indexOf(idList.length()-1);
        Integer id = Integer.parseInt(JSON.parseObject(data).getString("id"));
        System.out.println("********************************" + id);
        String where = JSON.parseObject(data).getString("transTowhere");
        System.out.println("********************************" + where);
        String time = JSON.parseObject(data).getString("transTime");
        System.out.println("********************************" + time);
        String person = JSON.parseObject(data).getString("transPhysician");
        String remark = JSON.parseObject(data).getString("transRemark");
        Transfer transfer = transferService.findTransferInfoById(id);
//        System.out.println("********************************" + transfer);
        transfer.setTransTowhere(where);
        transfer.setTransTime(new SimpleDateFormat("yyyy-MM-dd").parse(time));
        transfer.setTransPhysician(person);
        transfer.setTransRemark(remark);
//        System.out.println("********************************" + transfer);
        return transferService.updateInfo(transfer);
    }
}
